package com.smartwasser.yunhtgl.apphtgl.asps;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.activity.ImageLookActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.function.OfficeReader;
import com.smartwasser.yunhtgl.apphtgl.photo.ImagePagerActivity;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat1Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat1PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.view.SPGreat1View;
import com.smartwasser.yunhtgl.apphtgl.widget.QianzigaizhangView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.smartwasser.yunhtgl.apphtgl.network.Retrofits.base;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getPath;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getQicaoType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getSZType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getType;

/**
 * 已归档合同详情接口
 * 已归档合同
 * 用印合同详情
 */
public class SPCase3Activity extends BaseActivity implements SPGreat1View {
    private SPGreat1Presenter sp1Case3Presenter = new SPGreat1PresenterImpl(this);
    private SPGreat1Bean spGreat1Bean;
    private Dialog loadingHorizontal;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp1_normal_scroll)
    ScrollView sp1_normal_scroll;
    @BindView(R.id.sp1_normal_name)
    TextView sp1_normal_name;//合同名称
    @BindView(R.id.sp1_normal_code)
    TextView sp1_normal_code;//合同编号
    @BindView(R.id.sp1_great_sq)
    TextView sp1_great_sq;//合同申请部室
    @BindView(R.id.sp1_great_zgy)
    TextView sp1_great_zgy;//合同专管员
    @BindView(R.id.sp1_great_creat_time)
    TextView sp1_great_creat_time;//合同申请时间
    @BindView(R.id.sp1_great_fen)
    TextView sp1_great_fen;//合同份数
    @BindView(R.id.sp1_normal_main)
    TextView sp1_normal_main;//授权签署人
    @BindView(R.id.sp1_normal_yj)
    TextView sp1_normal_yj;//单位负责人意见
    @BindView(R.id.sp1_normal_fg)
    TextView sp1_normal_fg;//集团分管领导
    @BindView(R.id.sp1_normal_fg_yj)
    TextView sp1_normal_fg_yj;//集团分管领导意见
    @BindView(R.id.sp1_great_bz)
    TextView sp1_great_bz;//备注
    @BindView(R.id.sp1_great_qc)
    TextView sp1_great_qc;//起草
    @BindView(R.id.sp1_great_zw)
    TextView sp1_great_zw;//正文
    @BindView(R.id.sp1_great_yj)
    TextView sp1_great_yj;//法律意见书
    @BindView(R.id.sp1_great_sz)
    TextView sp1_great_sz;//收支情况
    @BindView(R.id.sp1_great_lx)
    TextView sp1_great_lx;//合同类型
    @BindView(R.id.sp1_great_qz)
    TextView sp1_great_qz;//签字盖章
    @BindView(R.id.sp1_great_fl_all)
    LinearLayout sp1_great_fl_all;
    private ArrayList<String> imageUrls = new ArrayList<>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp_case2);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("用印合同详情");
        sp1_normal_scroll.setVisibility(View.INVISIBLE);
        id = getIntent().getStringExtra("id");
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
        try {
            sp1Case3Presenter.getType1Sp1Great(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.sp1_great_zw_all})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sp1_great_zw_all:
                if (null != spGreat1Bean.getBuContractBaseInfo()) {
                    String path = spGreat1Bean.getBuContractBaseInfo().getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
        }
    }

    @Override
    public void success(SPGreat1Bean tb) {
        if (null != tb && null != tb.getBuContractBaseInfo()) {
            this.spGreat1Bean = tb;
            sp1_normal_code.setText(tb.getBuContractBaseInfo().getCode());
            sp1_normal_name.setText(tb.getBuContractBaseInfo().getName());
            sp1_great_lx.setText(getType(tb.getBuContractBaseInfo().getType()));
            sp1_great_qc.setText(getQicaoType(tb.getBuContractBaseInfo().getQicaoType()));
            sp1_great_zw.setText(getPath(tb.getBuContractBaseInfo().getPath()));
            sp1_great_sz.setText(getSZType(tb.getBuContractBaseInfo().getShouzhiType()));
            String qzht = tb.getBuContractBaseInfo().getQianziContract();
            if (!qzht.isEmpty() && qzht.length() > 0) {
                final String[] qiht=qzht.split("\\|");
                for(int i=1;i<qiht.length;i++){
                    imageUrls.add(base+qiht[i]);
                    QianzigaizhangView qianzigaizhangView=new QianzigaizhangView(SPCase3Activity.this,null);
                    String[] qzgz_array = qiht[i].split("/");
                    qianzigaizhangView.setText(qzgz_array[qzgz_array.length-1]);
                    if (i==1)
                        qianzigaizhangView.setVisibilityTitle();
                    sp1_great_fl_all.addView(qianzigaizhangView);
                    final int finalI = i;
                    qianzigaizhangView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (null != spGreat1Bean.getBuContractBaseInfo()) {
                                String path = qiht[finalI];
                                if (!path.isEmpty() && path.length() > 0) {
                                    if(path.contains(".JPG")||path.contains(".PNG")||path.contains(".jpg")||path.contains(".png")){
                                        imageBrower(finalI-1);
//                                        startActivity(ImageLookActivity.class, "url", path);
                                    }else{
                                        startActivity(OfficeReader.class, "url", path);
                                    }
                                } else {
                                    showResult("暂无数据");
                                }
                            } else {
                                showResult("暂无数据");
                            }
                        }
                    });
                }
            }
            if (null != tb.getBuContractBaseInfo().getBuMajorContractSignInfoList() && tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().size() > 0) {
                sp1_great_fen.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractCount() + "份");
                sp1_great_sq.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDept().getName());
                sp1_great_zgy.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_great_creat_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDate());
                sp1_great_bz.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getRemarks());
                sp1_normal_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_normal_yj.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractDwOfficerOpinion());
                sp1_normal_fg.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeLeader().getName());
                sp1_normal_fg_yj.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeOpinion());
            }
        }
        sp1_normal_scroll.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }

    private void imageBrower(int position) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, imageUrls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        startActivity(intent);
    }
    @Override
    public void successGetFlMessage(FlMessageBean flMessageBean) {

    }

    @Override
    public void successCommitSH(BaseBean baseBean) {

    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }
}